package org.lds.ers.client.widgets;

import java.util.List;

import org.lds.ers.client.StakeService;
import org.lds.ers.client.StakeServiceAsync;
import org.lds.ers.client.WardService;
import org.lds.ers.client.WardServiceAsync;
import org.lds.ers.client.model.StakeERSCInfo;
import org.lds.ers.client.model.StakeInfo;
import org.lds.ers.client.model.WardERSCInfo;
import org.lds.ers.client.model.WardInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MaintainWardERS extends VerticalPanel {
	private static MaintainWardERS singleton;
	public static MaintainWardERS getInstance()
	{
		if (singleton==null)
			singleton = new MaintainWardERS();
		
		return singleton;
	}
	
	final private Label lblBanner;
	final private HorizontalPanel pnlMain;
	final private VerticalPanel pnlWard;
	final private VerticalPanel pnlERSMain;
	final private VerticalPanel pnlERS;

	final private ListBox cboStake; 
	final private ListBox lstWard ;
	
	final private TextBox txtUserID;
	final private TextBox txtRealName;
	
//	final private TextBox txtUserID;
//	final private TextBox txtRealName;

	private StakeServiceAsync stakeService = GWT.create(StakeService.class);
	private WardServiceAsync wardService = GWT.create(WardService.class);
	
	public MaintainWardERS() {
		this.lblBanner = new Label("[[管理支會 ERSC 專員]]");
		this.add(this.lblBanner);
		
		this.pnlMain = new HorizontalPanel();
		this.pnlWard  = new VerticalPanel();
		this.pnlERSMain = new VerticalPanel();
		this.pnlMain.add(this.pnlWard);
		this.pnlMain.add(this.pnlERSMain);
		
		this.cboStake = new ListBox();
		this.pnlWard.add(this.cboStake);
		this.lstWard = new ListBox(true);
		this.lstWard.setWidth("150px");
		this.pnlWard.add(this.lstWard);
		
		this.add(this.pnlMain);
		
		cboStake.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				// TODO Auto-generated method stub
				refreshWard();
			}
		});
		
		lstWard.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				// TODO Auto-generated method stub
				refreshWardERSC();
			}
		});
		
		//加入右邊 ERS Panel
		this.pnlERS = new VerticalPanel();
		this.pnlERSMain.add(this.pnlERS);
		
		
		
		
		//Add a panel for insert new ERS records.
		final DecoratorPanel pnlAddNew = new DecoratorPanel();
		pnlAddNew.setVisible(false);
		final Button btnAdd = new Button("儲存");
		btnAdd.setEnabled(false);
		
		FlexTable editForm = new FlexTable();
		editForm.setWidget(0, 0, new Label("使用者ID :"));
		this.txtUserID = new TextBox();
		this.txtUserID.addKeyPressHandler(new KeyPressHandler(){
			public void onKeyPress(KeyPressEvent event) {
				btnAdd.setEnabled(!txtUserID.getText().equals(""));
			}
		});
		editForm.setWidget(0,1,this.txtUserID);
		
		editForm.setWidget(1, 0, new Label("使用者姓名 :"));
		this.txtRealName = new TextBox();
		editForm.setWidget(1,1,this.txtRealName);
		
		btnAdd.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				btnAdd.setEnabled(false);
				if (cboStake.getSelectedIndex() > -1)
				{
					WardERSCInfo ersc = new WardERSCInfo();
					ersc.setUserID(txtUserID.getText());
					ersc.setRealName(txtRealName.getText());
					ersc.setWardID(Long.parseLong(lstWard.getValue(lstWard.getSelectedIndex())));
				
					wardService.insertERSC(ersc, new AsyncCallback<WardERSCInfo>() {

						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							btnAdd.setEnabled(true);
							Window.alert(caught.getMessage());
						}

						public void onSuccess(WardERSCInfo result) {
							// TODO Auto-generated method stub
							txtUserID.setText("");
							txtRealName.setText("");
							btnAdd.setEnabled(false);
							pnlAddNew.setVisible(false);
							refreshWardERSC();
						}
				    });
				}
			}
		});
		editForm.setWidget(1,2, btnAdd);
		final Button btnCancel = new Button("取消");
		btnCancel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				pnlAddNew.setVisible(false);
				txtUserID.setText("");
				txtRealName.setText("");
			}
		});
		editForm.setWidget(1,3,btnCancel);
		
		pnlAddNew.add(editForm);
		
		
		final Button btnShowAddNew = new Button("新增");
		btnShowAddNew.addClickHandler(new ClickHandler(){

			public void onClick(ClickEvent event) {
				pnlAddNew.setVisible(true);
			}
		});
		
		this.pnlERSMain.add(btnShowAddNew);
		this.pnlERSMain.add(pnlAddNew);
		
		
		
		
		
		refreshStakes();
		
	}
	
	public void refreshStakes()
	{
		stakeService.getAll(new AsyncCallback<List<StakeInfo>>() {

			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert(caught.getMessage());
			}

			public void onSuccess(List<StakeInfo> result) {
				// TODO Auto-generated method stub
				cboStake.clear();
				for(StakeInfo s : result) {
					cboStake.addItem(s.getStakeName() + "(" + s.getStakeNo() + ")", s.getId() + "");
				}
				if (result.size() > 0)
					cboStake.setItemSelected(0, true);
				refreshWard();
			}
	    });
	}
	
	public void refreshWard()
	{
		this.lstWard.clear();
		if (cboStake.getSelectedIndex() > -1)
		{
			Long stakeID = Long.parseLong(this.cboStake.getValue(this.cboStake.getSelectedIndex()));
			wardService.getAll(stakeID , new AsyncCallback<List<WardInfo>>() {
	
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					Window.alert(caught.getMessage());
				}
	
				public void onSuccess(List<WardInfo> result) {
					// TODO Auto-generated method stub
					
					for(WardInfo s : result) {
						lstWard.addItem(s.getWardName() + "(" + s.getWardNo() + ")", s.getId() + "");
					}
					if (result.size() > 0)
						lstWard.setItemSelected(0, true);
					refreshWardERSC();
				}
		    });

		}
	}
	
	public void refreshWardERSC()
	{
		if (this.lstWard.getSelectedIndex() == -1)
			return ;
		Long wardID = Long.parseLong(this.lstWard.getValue(this.lstWard.getSelectedIndex()));
		wardService.getAllERSC(wardID, new AsyncCallback<List<WardERSCInfo>>() {

			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			public void onSuccess(List<WardERSCInfo> result) {
				pnlERS.clear();
				final FlexTable flexTable =new FlexTable();
			    
			    flexTable.addStyleName("cw-FlexTable");
			    flexTable.setWidth("32em");
			    flexTable.setBorderWidth(1);
			    flexTable.setCellSpacing(0);
			    flexTable.setCellPadding(3);
			    flexTable.setText(0, 0, "ERS專員代碼：" );
				flexTable.setText(0, 1, "ERS專員姓名：" );
				flexTable.setText(0, 2, "");
			    
			    int row = 1;
				for(WardERSCInfo s : result )
				{
					addRow(row, flexTable, s);
					row += 1;
				}
				pnlERS.add(flexTable);
				
			}
	    });	
	}
	
	private void addRow(int row, FlexTable table, WardERSCInfo info)
	{
		final WardERSCInfo ersc = info;
		table.setText(row, 0, ersc.getUserID() );
		table.setText(row, 1, ersc.getRealName() );
		
		final Button btnDelete = new Button("移除");
		btnDelete.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (Window.confirm("您確定要移除這位 ERSC 專員：『" + ersc.getRealName() + "(" +  ersc.getUserID()  + ")』"))
				{
					btnDelete.setEnabled(false);
					wardService.deleteERSC(ersc.getID(), new AsyncCallback<Boolean>() {
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							btnDelete.setEnabled(true);
							Window.alert(caught.getMessage());
						}

						public void onSuccess(Boolean result) {
							
							refreshWardERSC();
						}
				    });
				}
			}
		});
		
		table.setWidget(row, 2, btnDelete );
	}

	
}
